package com.home.andromo;

import java.util.List;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AppInfoAdapterSeleccion extends BaseAdapter {
	private Context mContext;
	private List mListAppInfo;
	private PackageManager mPackManager;

	public AppInfoAdapterSeleccion(Context c, List list, PackageManager pm) {
		mContext = c;
		mListAppInfo = list;
		mPackManager = pm;
	}

	@Override
	public int getCount() {
		return mListAppInfo.size();
	}

	@Override
	public Object getItem(int position) {
		return mListAppInfo.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// get the selected entry
		ApplicationInfo entry = ((Application) mListAppInfo.get(position)).getApp();

		// reference to convertView
		View v = convertView;

		// inflate new layout if null
		if(v == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			v = inflater.inflate(R.layout.seleccion_apps, null);
		}

		// load controls from layout resources
		ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon);
		TextView tvAppName = (TextView)v.findViewById(R.id.tvName);
		TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack);

		try {
			// set data to display
			Drawable icono = entry.loadIcon(mPackManager);
			// Read your drawable from somewhere
			Bitmap bitmap = ((BitmapDrawable) icono).getBitmap();
			// Scale it to 50 x 50
			Drawable d = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap, 50, 50, true));
			// Set your new, scaled drawable "d"
			
			ivAppIcon.setImageDrawable(d);
			
					tvAppName.setText(entry.loadLabel(mPackManager));
			tvPkgName.setText(entry.packageName);	
		} catch (Exception e) {
			// ignore
		}
		

		// return view
		return v;
	}
}
